<?php

class User extends DataMapper {

	public $has_many = array('org');
	//public $has_one = array('primaryorg'=> array ('class'=>"orgs"));
	private $CI;
	function User()
	{
		parent::DataMapper();
		$this->CI =& get_instance();
		//$settings = $this->CI->sessionhelper->getSettings();
		//print_r($settings);
	}
	function loggedIn()
	{
		$settings = $this->CI->sessionhelper->getSettings();
		if (isset($settings['userid']))
			return $settings['userid'];
		else
			return -1;
		
	}
	function login($user, $pass)
	{
		$u = new User();
		$u->where("username", $user)->where("password", md5($pass))->get();
		if ($u->exists())
		{
			$settings["userid"] = $u->id;
			$this->CI->sessionhelper->writeSettings($settings);
			return true;
		} else {
			return false;
		}
		
	}
	function getUser()
	{
		$settings = $this->CI->sessionhelper->getSettings();
		if (isset($settings['userid']))
		{
			$u = new User();
			$u->where("id", $settings['userid'])->get();
			return $u;
		} else {
			return NULL;	
		}
			
	}
}

?>